package org.lalib.controller.action;

import java.util.ArrayList;
import java.util.List;

import org.lalib.acquire.IControllerAction;
import org.lalib.controller.form.CUserCategoryForm;
import org.lalib.controller.form.CUserCategoryForm.CategoryBean;
import org.lalib.controller.service.CCategoryService;
import org.lalib.controller.service.CUserService;
import org.lalib.entity.object.ECategory;
import org.lalib.entity.object.EItem;
import org.lalib.entity.object.EUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

@Scope("request")
@Controller("cUserCategoryAction")
public class CUserCategoryAction implements IControllerAction {

	private static final long serialVersionUID = 1L;

	@Autowired
	CUserCategoryForm cUserCategoryForm;

	@Autowired
	CCategoryService cCategoryService;

	@Autowired
	CUserService cUserService;

	public String init(EItem item) {
		EUser user = (EUser) item;
		resetFormVariables(user);
		return "userCategory";
	}

	private void resetFormVariables(EUser user) {
		user = (EUser) cUserService.getUserById(user.getId());
		List<ECategory> userCategories = user.getCategories();
		List<ECategory> allUserCategories = cCategoryService.getAllUserCategories();
		List<ECategory> filteredCategories = cCategoryService.filterCategories(userCategories, allUserCategories);

		List<CategoryBean> userBaseCategoryBeans = getCategoryBeansForGivenCategories(userCategories);
		List<CategoryBean> excludedBaseCategoryBeans = getCategoryBeansForGivenCategories(filteredCategories);

		cUserCategoryForm.setUser(user);
		cUserCategoryForm.setUserCategories(userBaseCategoryBeans);
		cUserCategoryForm.setExcludedCategories(excludedBaseCategoryBeans);
		cUserCategoryForm.setExcludedBaseCategories(filteredCategories);
		cUserCategoryForm.setUserBaseCategories(userCategories);
	}

	public void addToUser() {
		List<CategoryBean> selectedCategoryBeans = cUserCategoryForm.getExcludedCategories();
		EUser user = cUserCategoryForm.getUser();
		user = cUserService.getUserById(user.getId());

		boolean check = false;
		for (CategoryBean categoryBean : selectedCategoryBeans) {
			if (categoryBean.isSelected()) {
				user.getCategories().add(categoryBean.getCategory());
				check = true;
			}
		}

		if (check) {
			cUserService.update(user);
			resetFormVariables(user);
		}
	}

	public void removeFromUser() {
		List<CategoryBean> selectedCategoryBeans = cUserCategoryForm.getUserCategories();
		EUser user = cUserCategoryForm.getUser();
		user = cUserService.getUserById(user.getId());

		boolean check = false;
		for (CategoryBean categoryBean : selectedCategoryBeans) {
			if (categoryBean.isSelected()) {
				deleteUserCategoryForCategory(user, categoryBean.getCategory());
				check = true;
			}
		}

		if (check) {
			cUserService.update(user);
			resetFormVariables(user);
		}
	}

	private void deleteUserCategoryForCategory(EUser user, ECategory category) {
		int id = 0;
		boolean found = false;

		for (int i = 0; i < user.getCategories().size(); i++) {
			if (user.getCategories().get(i).getId().equals(category.getId())) {
				id = i;
				found = true;
			}
		}

		if (found)
			user.getCategories().remove(id);
	}

	private List<CategoryBean> getCategoryBeansForGivenCategories(List<ECategory> categories) {
		List<CategoryBean> categoryBeans = new ArrayList<>();
		for (ECategory category : categories)
			categoryBeans.add(cUserCategoryForm.new CategoryBean(category));
		return categoryBeans;
	}
}